package com.haotunet.app.goldtreasure.adapter;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.RelativeSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.model.RecommendProductBean;
import com.haotunet.app.goldtreasure.utils.ActivityUtils;

import java.util.List;

public class RecommendGoldAdapter extends BaseAdapter {
    private List<RecommendProductBean> list;

    private Context mContext;

    public RecommendGoldAdapter(Context context, List<RecommendProductBean> listData) {
        mContext = context;
        list = listData;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_homepage_regular_gold, null);
            holder = new ViewHolder();
            holder.tvProductTitle = (TextView) convertView.findViewById(R.id.recommend_regular_title);
            holder.tvProductDiscount = (TextView) convertView.findViewById(R.id.recommend_regular_rate);
            holder.tvProductIntroduce1 = (TextView) convertView.findViewById(R.id.regular_introduce1);
            holder.tvProductIntroduce2 = (TextView) convertView.findViewById(R.id.regular_introduce2);
            holder.tvProductIntroduce3 = (TextView) convertView.findViewById(R.id.regular_introduce3);
            holder.tvBtnText = (TextView) convertView.findViewById(R.id.btn_regular_text);
            holder.mProductLogo = (ImageView) convertView.findViewById(R.id.product_type_logo);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        RecommendProductBean productBean = list.get(position);
        final String productName = productBean.getName();
        holder.tvProductTitle.setText(productName);
        if (!TextUtils.isEmpty(productBean.getIconUrl())) {
            holder.mProductLogo.setVisibility(View.VISIBLE);
            Glide.with(mContext).load(productBean.getIconUrl()).into(holder.mProductLogo);
        }else{
            holder.mProductLogo.setVisibility(View.GONE);
        }
        final int productFlag = productBean.getProductType();
        final String buyGoldH5url = productBean.getBuyGoldH5url();
        String rateExtDesc = productBean.getRateExtDesc();
        if (productFlag == 2) {
            SpannableStringBuilder style = new SpannableStringBuilder(rateExtDesc);
            style.setSpan(new RelativeSizeSpan(0.5f), 0, 2, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            style.setSpan(new RelativeSizeSpan(0.5f), rateExtDesc.length() - 3, rateExtDesc.length(),
                    Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            holder.tvProductDiscount.setText(style);
        } else {
            SpannableStringBuilder style1 = new SpannableStringBuilder(rateExtDesc);
            style1.setSpan(new RelativeSizeSpan(0.5f), rateExtDesc.length() - 1, rateExtDesc.length(),
                    Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            holder.tvProductDiscount.setText(style1);
        }
        holder.tvProductIntroduce1.setText(productBean.getRateIntroduction());
        holder.tvProductIntroduce2.setText(productBean.getBaselineDesc());
        holder.tvProductIntroduce3.setText(productBean.getIntroduction());
        if (productBean.getIsDisable()) {
            holder.tvBtnText.setText("下期再见");

        } else {
            holder.tvBtnText.setText("买入");
        }
        convertView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                ActivityUtils.gotoDynamicDetail(mContext, buyGoldH5url, 11);
            }
        });
        return convertView;
    }

    class ViewHolder {
        TextView tvProductTitle;
        TextView tvProductDiscount;
        TextView tvProductIntroduce1;
        TextView tvProductIntroduce2;
        TextView tvProductIntroduce3;
        TextView tvBtnText;
        ImageView mProductLogo;
    }
}
